import UIKit

extension UIImage {
    
    func compress() -> Data? {
        let maxLength: CGFloat = 500 * 1024
        guard var newImage = self.compress(with: CGSize(width: 960, height: 960 * self.size.height / self.size.width)) else {
            return nil
        }
        
        var imageData = newImage.jpegData(compressionQuality: 1.0)
        var imageDataLen = imageData?.count ?? 0
        
        if imageDataLen <= Int(maxLength) {
            return imageData
        } else {
            imageData = newImage.jpegData(compressionQuality: 0.99)
            imageDataLen = imageData?.count ?? 0
            
            if imageDataLen < Int(maxLength) {
                return imageData
            }
            
            var maxQuality: CGFloat = 1.0
            var minQuality: CGFloat = 0.0
            var flag = 0
            
            while true {
                let midQuality = (maxQuality + minQuality) / 2
                if flag == 6 {
                    return newImage.jpegData(compressionQuality: minQuality)
                }
                flag += 1
                if let imageData = newImage.jpegData(compressionQuality: midQuality) {
                    let len = imageData.count
                    if len > Int(maxLength) {
                        maxQuality = midQuality
                    } else if len < Int(maxLength) {
                        minQuality = midQuality
                    } else {
                        return imageData
                    }
                }
            }
        }
    }
    
    func compress(with size: CGSize) -> UIImage? {
        UIGraphicsBeginImageContext(size)
        self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
}

extension String {
    static let hxm: [String: String] = [:]
    
    var hx: String {
        return String.hxm[self] ?? self
    }
    
    var rhx: String {
        return self
    }
    
}


extension Optional {
   
   
   var asInt: Int {
           switch self {
           case .some(let value as Int):
               return value
           case .some(let value as String):
               if let intValue = Int(value) {
                   return intValue
               }
               return 0 // 如果无法转换为整数，则返回默认值
           case .some(let value as Double):
               return Int(value)
           case .some(let value as Bool):
               return value ? 1 : 0
           case .none:
               return 0
           default:
               if let value = self as? CustomStringConvertible {
                   if let intValue = Int(value.description) {
                       return intValue
                   }
               }
               return 0
           }
       }
   var asString: String {
           switch self {
           case .some(let value as Int):
               return String(value)
           case .some(let value as String):
               return value
           case .some(let value as Double):
               return String(value)
           case .some(let value as Bool):
               return String(value)
           case .none:
               return ""
           default:
               return String(describing: self)
           }
       }
       
       var asListDic: Array<Dictionary<String, Any?>> {
           if let array = self as? Array<Dictionary<String, Any?>> {
               return array
           } else {
               return []
           }
       }
       
   var asDic: [String: Any?] {
          get {
              if let dic = self as? [String: Any?] {
                  return dic
              } else {
                  return [:]
              }
          }
          set {
              guard var dic = self as? [String: Any?] else { return }
              dic = newValue
              self = dic as? Wrapped
          }
      }
}
